package com.lin.services.impl;

import com.lin.dao.SaleDao;
import com.lin.services.SaleService;
import com.lin.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SaleServiceImpl implements SaleService {
    @Autowired
    private SaleDao saleDao;

    @Override
    public List<Map<String, Object>> selectSaleWaresInfo(String waresInfo, String promotionInfo, String promotionType) {
        String waresInfoRe = null;
        String promotionInfoRe = null;
        String promotionTypeRe = null;
        if (waresInfo != null && !"".equals(waresInfo.trim())) {
            waresInfoRe = waresInfo;
        }
        if (promotionInfo != null && !"".equals(promotionInfo.trim())) {
            promotionInfoRe = promotionInfo;
        }
        if (promotionType != null && !"all".equals(promotionType) && !"".equals(promotionType.trim())) {
            promotionTypeRe = promotionType;
        }
        List<Map<String, Object>> list = saleDao.selectSaleWaresInfo(waresInfoRe, promotionInfoRe,
                promotionTypeRe, ShiroUtil.getUser().getStoreId());
        for (Map<String, Object> map : list) {
            map.put("promotionStartDate", map.get("promotionStartDate").toString());
            map.put("promotionEndDate", map.get("promotionEndDate").toString());
        }
        return list;
    }

    @Override
    public Map<String, Object> delSaleWares(String promotionId, String waresId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", false);
        map.put("msg", "操作无效");
        if (promotionId == null || "".equals(promotionId.trim())) {
            return map;
        }
        if (waresId == null || "".equals(waresId.trim())) {
            return map;
        }
        int i = saleDao.delSaleWares(promotionId, waresId);
        if (i == 1) {
            map.clear();
            map.put("code", true);
            map.put("msg", "操作成功");
        }
        return map;
    }
}
